//封装请求接口
//导入axios
import axios from 'axios'
//配置基准路径
// ;(axios.defaults.baseURL = 'http://klxin.cn:8888/api/private/v1'),
// axios.defaults.baseURL = 'http://klxin.cn:115.159.87.220/api/private/v1'
axios.defaults.baseURL = 'http://47.94.247.66:8888/api/private/v1'

// 添加请求拦截器
axios.interceptors.request.use(
  function (config) {
    // 在发送请求之前做些什么
    var token = window.sessionStorage.getItem('token')
    // console.log(token);
    // console.log(token);
    config.headers['Authorization'] = token

    //下载测试
    // config.headers.Authoriz  ation = localStorage.getItem('token')
    return config
  },
  function (error) {
    // 对请求错误做些什么
    return Promise.reject(error)
  }
)

// 添加响应拦截器
axios.interceptors.response.use(
  function (response) {
    // 对响应数据做点什么
    return response
  },
  function (error) {
    // 对响应错误做点什么
    return Promise.reject(error)
  }
)

//1-登录请求函数
export const login_api = obj => {
  return axios.post('/login', obj)
}
//2. 左侧菜单获取
export const menus_api = () => {
  return axios('/menus')
}
//3. user数据获取
export const users_api = params => {
  return axios('/users', {
    params
  })
}
//4. 状态开关请求
export const usersBtn_api = obj => {
  return axios.put(`users/${obj.uId}/state/${obj.type}`)
}
//5.  删除用户请求
export const usersRemove_api = id => {
  return axios.delete(`users/${id}`)
}
//6. 编辑请求
export const redact_api = obj => {
  return axios.put(`/users/${obj.id}`, {
    email: obj.eml,
    mobile: obj.phone
  })
}
//7. 添加用户
export const adduser_api = obj => {
  return axios.post('/users', {
    username: obj.name,
    password: obj.password,
    email: obj.eml,
    mobile: obj.phone
  })
}
//8. 角色权限列表数据获取
export const rights_api = type => {
  return axios(`/rights/${type}`)
}

//9.角色列表数据获取
export const roles_api = () => {
  return axios.get('/roles')
}
//10. 删除角色指定权限
export const remove_api = obj => {
  return axios.delete(`/roles/${obj.roleId}/rights/${obj.rightId}`)
}
//11.给角色授权 最后写  请求路径：roles/:roleId/rights
export const rolesUser_api = obj => {
  return axios.post(`roles/${obj.roleId}/rights`, {
    rids: obj.rids
  })
}
//12. 商品列表数据
export const goods_api = params => {
  return axios.get('/goods', {
    params
  })
}
//13 删除商品
export const removegoods_api = id => {
  return axios.delete(`/goods/${id}`)
}
//14. 商品分类
export const cate_api = obj => {
  return axios.get(`categories`, {
    params: {
      type: obj.type,
      pagenum: obj.pagenum,
      pagesize: obj.pagesize
    }
  })
}
//15 获取商品参数列表
export const categories_api = obj => {
  return axios(`categories/${obj.id}/attributes`, {
    params: {
      sel: obj.sel
    }
  })
}
//16. 获取商品分类列表
export const categoriesSort_api = obj => {
  return axios('categories', {
    params: {
      type: obj.type,
      pagenum: obj.pagenum,
      pagesize: obj.pagesize
    }
  })
}
//17. 图片上传
export const upload_api = obj => {
  return axios.post('upload')
}
